package com.portalito.jahepi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.telephony.TelephonyManager;

public class Utils {
	
	public static boolean estaVacio(String value) {
		if(value != null) {
			return value.length() == 0;
		}
		return true;
	}
	
	public static boolean estaVacio(int[] value) {
		if(value != null) {
			return value.length == 0;
		}
		return true;
	}
	
	public static boolean esEntero(String value) {
		try {
			Integer.parseInt(value);
		} catch(NumberFormatException e) {
			return false;
		}
		return true;
	}
	
	public static String implode(String[] ary, String delim) {
	    String out = "";
	    for(int i = 0; i < ary.length; i++) {
	        if(i!=0) { out += delim; }
	        out += ary[i];
	    }
	    return out;
	}

	public static String SHA1(String text) { 
		String hash = "";
		try {
			MessageDigest md;
		    byte[] buffer, digest;
	
			buffer = text.getBytes();
	        md = MessageDigest.getInstance("SHA1");
	        md.update(buffer);
	        digest = md.digest();
	
	        for(byte aux : digest) {
	            int b = aux & 0xff;
	            if (Integer.toHexString(b).length() == 1) hash += "0";
	            hash += Integer.toHexString(b);
	        }
		} catch(NoSuchAlgorithmException exp) {
	    	exp.printStackTrace();
	    }
		
        return hash;
	}
	
	public static String obtenerAndroidId(Context context) {
		TelephonyManager manager= (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		return manager.getDeviceId();
	}
	
	public static String obtenerFechaTiempoActual() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		Date date = new Date();
		return dateFormat.format(date);
	}
	
	public static String obtenerFechaActual() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
		Date date = new Date();
		return dateFormat.format(date);
	}
	
	public static Bitmap decodeFile(File f){
	    try {
	        //Decode image size
	        BitmapFactory.Options o = new BitmapFactory.Options();
	        o.inJustDecodeBounds = true;
	        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

	        //The new size we want to scale to
	        final int REQUIRED_SIZE = 70;

	        //Find the correct scale value. It should be the power of 2.
	        int width_tmp = o.outWidth, height_tmp = o.outHeight;
	        int scale = 1;
	        while(true) {
	            if(width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) {
	            	break;
	            }
	            width_tmp /= 2;
	            height_tmp /= 2;
	            scale *= 2;
	        }

	        //Decode with inSampleSize
	        BitmapFactory.Options o2 = new BitmapFactory.Options();
	        o2.inSampleSize = scale;
	        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
	    } catch (FileNotFoundException e) {
	    	e.printStackTrace();
	    }
	    return null;
	}
	
	public static String limpiarFecha(String f) {
		try {
			String[] partes = f.split(" ");
			String[] fecha = partes[0].split("-");
			return fecha[2] + fecha[1] + fecha[0].substring(fecha[0].length() - 2);
		} catch(Exception e) {}
		return "";
	}
	
	public static int stringToInt(String n) {
		try {
			return Integer.valueOf(n);
		} catch(Exception e) {}
		return 0;
	}
	
	public static double stringToDouble(String n) {
		try {
			return Double.valueOf(n);
		} catch(Exception e) {}
		return 0;
	}
	
	public static String obtenerHora(String fecha) {
		String[] partes = fecha.split(" ");
		if(partes.length == 2) {
			return partes[1];
		}
		return "";
	}
	
	public static String obtenerDia(String fecha) {
		String[] partes = fecha.split(" ");
		if(partes.length == 2) {
			String[] partesDia = partes[0].split("-");
			if(partesDia.length == 3) {
				return partesDia[2] + "-" + partesDia[1] + "-" + partesDia[0];
			}
		}
		return "";
	}
}
